<template>
  <div class="main-box">
    <div>
      <!-- 左侧的图片 -->
      <div class="image-box">
        <img :src="good.imageUrl" class="image">
      </div>
      <!-- 右侧盒子 -->
      <div class="detail-box">
        <!-- 商品名与描述 -->
        <div>
          <span style="font-size: 22px"><strong>{{ good.goodsName }}</strong></span><br />
        </div>
        <div style="margin-top: 20px">
          <span style="font-size: 17px;color: red">{{ good.goodsId }}</span>
        </div>
        <!-- 价格盒子 -->
        <div class="price-box">
          <dl>
            <div>
              <dt>价格</dt>
              <dd style="color: red;font-size: 25px">{{ good.goodsSellPrice }}元</dd>
            </div>
          </dl>
        </div>
        <!-- 月销量 -->
        <div style="margin-top: 20px">
          <span>月销量：</span>
          <span style="color: red">{{ good.currentStorage }}</span><br />
          <span style="height: 40px" v-if="showStore">库存：{{ good.currentStorage }}</span>
        </div>
        <!-- 选择数量 -->
        <div style="margin-top: 20px">
          <el-input-number v-model="count" controls-position="right" :min="1"
            :max="good.currentStorage"></el-input-number>
        </div>
        <!-- 购买按钮组 -->
        <div style="margin-top: 30px">
          <el-button class="button" @click="buyItem">购买</el-button>
          <el-button class="button" @click="addToCart" icon="el-icon-edit">加入购物车</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      good: {},
      count: 1,
      showStore: true
    };
  },
  mounted() {
    this.goodId = this.$route.params.goodId;
    axios.get(`http://localhost:8089/consumer/selectGoodsDetail?storeId=${this.goodId}&goodsId=${this.goodId}`)
      .then(res => {
        if (res.data.code === 200) {
          this.good = res.data.data;
          this.good.goodsSellPrice = parseFloat(this.good.goodsSellPrice);
          this.count = Math.min(1, this.good.currentStorage);
        }
      });
  },
  methods: {
    buyItem() {
      const totalPrice = this.good.goodsSellPrice * this.count;
      this.$router.push({
        path: '/pay',
        query: {
          goodsId: this.good.goodsId,
          price: totalPrice,
          quantity: this.count
        }
      });
    },
    addToCart() {
      const totalPrice = this.good.goodsSellPrice * this.count;
      this.$router.push({
        path: '/cart',
        query: {
          goodsId: this.good.goodsId,
          price: totalPrice,
          quantity: this.count
        }
      });
    }
  }
};
</script>

<style scoped>
.main-box {
  width: 1060px;
  margin: 20px auto;
  padding: 30px;
  background-color: #ffffff;
  overflow: hidden;
}

.image-box {
  height: 420px;
  width: 420px;
  border: #f2f2f2 1px solid;
  text-align: center;
  margin-left: 80px;
  margin-top: 30px;
  display: inline-block;
  overflow: hidden;
}

.image {
  height: 100%;
  width: 350px;
}

.detail-box {

  width: 420px;
  display: inline-block;
  margin-left: 50px;
  overflow: hidden;
}

.price-box {
  background-color: #e9e9e9;
  background-image: linear-gradient(to top right, #e3fafa, rgba(159, 250, 246, 0.56));
  border-radius: 5px;
  font: 12px/1.5 "Microsoft Yahei", tahoma, arial;
  padding-bottom: 1px;
  padding-top: 1px;
  margin-right: 20px;
  margin-top: 30px;
}

.price-box div {
  line-height: 20px;
  margin-left: 8px;
  margin-bottom: 5px;
}

.price-box dl dt {
  float: left;
  font-size: 14px;
  line-height: 20px;
}

.price-box dl dd {
  font-size: 18px;
  line-height: 20px;
}

.button {
  width: 130px;
  height: 45px;
  background-color: #96e2e0;
  color: #710a0a;
}

.standard {
  height: 30px;
  margin-right: 10px;
}
</style>